using System;
using System.Collections.Generic;
using System.Text;

using Mvp.Xml.Common.Xsl;
using System.Xml.Xsl;
using System.IO;
using Instedd.Labs.Geo.Library.Sms;

namespace Instedd.Labs.Geo.Library.Rss
{
    [Obsolete("The Geo Rss Manager should not be used to generate RSS feed anymore. Create the strongly type RSS object model and use the RssWriter.")]
    public class GeoRssManager
    {
        private readonly string KmlToGeoRssXslPath;
        const string DefaultKmlToGeoRssXslFileName = "KmlToGeoRss.xslt";

        const string PlacemarkNameParameterName = "placemarkName";

        public GeoRssManager()
            : this(DefaultKmlToGeoRssXslFileName)
        { }

        public GeoRssManager(string kmlToGeoRssXslPath)
        {
            this.KmlToGeoRssXslPath = PathResolver.TryToResolveUsingBaseDirectory(kmlToGeoRssXslPath);

            if (!File.Exists(this.KmlToGeoRssXslPath))
            {
                // Move to Resources
                throw new FileNotFoundException(string.Format("The kml to GeoRss XSL file name '{0}' was not found.", this.KmlToGeoRssXslPath));
            }
        }

        public void WriteGeoRss(string kmlUri, string outputUri)
        {
            WriteGeoRss(kmlUri, outputUri, string.Empty);
        }

        public void WriteGeoRss(string kmlUri, string outputUri, string placemarkName)
        {
            WriteGeoRss(new XmlInput(kmlUri), new XmlOutput(outputUri), placemarkName);
        }

        public void WriteGeoRss(XmlInput KmlFormat, XmlOutput output)
        {
            WriteGeoRss(KmlFormat, output, string.Empty);
        }

        public void WriteGeoRss(XmlInput KmlFormat, XmlOutput output, string placemarkName)
        {
            MvpXslTransform xslTransform = new MvpXslTransform();
            xslTransform.Load(KmlToGeoRssXslPath);

            XsltArgumentList argumentList = new XsltArgumentList();
            argumentList.AddParam(PlacemarkNameParameterName, string.Empty, placemarkName);

            xslTransform.Transform(KmlFormat, argumentList, output);
        }
    }
}
